package com.example.a33mvvm.fragment_homeviewpage;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.a33mvvm.R;
import com.example.a33mvvm.adapter.HomeVideoAdapter;
import com.example.a33mvvm.databinding.FragmentFIrstBinding;
import com.example.a33mvvm.home.BannerViewModel;
import com.example.base_mvvm.base.view.BaseFragment;
import com.example.base_mvvm.entity.HomeVideoEntity;
import com.example.base_mvvm.entity.LikeEntity;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends BaseFragment<FragmentFIrstBinding, BannerViewModel> {
    private List<HomeVideoEntity.DataBean> homeVideos = new ArrayList<>();
    private HomeVideoAdapter homeVideoAdapter;
    private int current_position = 0;

    @Override
    protected void initData() {
        viewModel.homeVideo(4, 10);
        homeVideos.clear();
        viewModel.homeVideo_success.observe(this, new Observer<HomeVideoEntity>() {
            @Override
            public void onChanged(HomeVideoEntity homeVideoEntity) {
                if (homeVideoEntity.getCode() == 200) {
                    homeVideos.addAll(homeVideoEntity.getData());
                    homeVideoAdapter.notifyDataSetChanged();
                } else {
                    Log.d("sss", homeVideoEntity.getMessage());
                }
            }
        });
        viewModel.homeVideo_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Log.d("sss", "onChanged: " + s);
            }
        });
        viewModel.like_success.observe(this, new Observer<LikeEntity>() {
            @Override
            public void onChanged(LikeEntity likeEntity) {
                if(likeEntity.getCode()==200){
                    ToastUtils.showLong(likeEntity.getMessage());
                    homeVideos.get(current_position).setDianzan(1);
                    homeVideoAdapter.notifyItemChanged(current_position);
                }
            }
        });
        viewModel.like_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        viewModel.disLike_success.observe(this, new Observer<LikeEntity>() {
            @Override
            public void onChanged(LikeEntity likeEntity) {
                if(likeEntity.getCode()==200){
                    ToastUtils.showLong(likeEntity.getMessage());
                    homeVideos.get(current_position).setDianzan(0);
                    homeVideoAdapter.notifyItemChanged(current_position);
                }
            }
        });
        viewModel.disLike_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }
    @Override
    protected void initView() {
        dataBinding.rec.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        homeVideoAdapter = new HomeVideoAdapter(homeVideos);
        dataBinding.rec.setAdapter(homeVideoAdapter);
        //dataBinding.rec.setLayoutManager(new GridLayoutManager(requireContext(),2));
        homeVideoAdapter.addChildClickViewIds(R.id.homeVideo_like);
        homeVideoAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                current_position = position;
                if (view.getId() == R.id.homeVideo_like) {
                    HomeVideoEntity.DataBean dataBean = homeVideos.get(current_position);
                    likeVideo(dataBean);
                }
            }
        });
    }
    private void likeVideo(HomeVideoEntity.DataBean dataBean) {
        if (dataBean.getDianzan() == 0) {
            viewModel.like(dataBean.getId());
        } else {
            viewModel.disLike(dataBean.getId());
        }
    }

    @Override
    protected Class<BannerViewModel> getViewModel() {
        Log.d("TAG", "getViewModel: "+"无敌");
        return BannerViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_f_irst;
    }
}